package com.varian.security.authorization;

import com.varian.security.handler.VarianAuthenticationEntryPoint;
import lombok.RequiredArgsConstructor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver;

/**
 * @author ben
 * @since 2024/6/23
 */
@RequiredArgsConstructor
public class VarianOAuth2ResourceServerConfigurer {

    private final JwtDecoder jwtDecoder;

    public void customize(OAuth2ResourceServerConfigurer<HttpSecurity> configurer) {
        configurer
                .bearerTokenResolver(new DefaultBearerTokenResolver())
                .authenticationEntryPoint(new VarianAuthenticationEntryPoint())
                .jwt(jwt -> jwt.decoder(jwtDecoder)
                        .jwtAuthenticationConverter(new VarianJwtBearerTokenAuthenticationConverter()));
    }
}
